package com.linwen.excise.niuke._60;

import java.util.*;

/**
 * @author wlw
 * @date 2023/3/16 10:12 PM
 */
public class _45名字的漂亮度 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        String[] strings = new String[n];
        for (int i = 0; i < n; i++) {
            strings[i] = scanner.nextLine();
        }
        for (String string : strings) {
            System.out.println(function(string));
        }
    }

    private static int function(String str) {
        int max = 26;
        int result = 0;
        char[] chars = str.toCharArray();
        Map<Character,Integer> map = new HashMap<>();
        for (char aChar : chars) {
            if(map.containsKey(aChar)){
                map.put(aChar,map.get(aChar)+1);
            }else{
                map.put(aChar,1);
            }
        }
        List<MyNode> list = new ArrayList<>();
        map.forEach((character, integer) -> list.add(new MyNode(character,integer)));
        list.sort((o1, o2) -> o2.count-o1.count);
        map.clear();
        for (MyNode node : list) {
            map.put(node.character,max--);
        }
        for (char aChar : chars) {
            result += map.get(aChar);
        }
        return result;
    }
    static class MyNode{
        Character character;
        int count;
        MyNode(Character character,int count){
            this.character = character;
            this.count = count;
        }
    }
}

